package view.backing.Utils.Validator;

import chemtools_logica.LN.IR.LN_SFContenedoresRemote;

import chemtools_logica.LN.IR.LN_SFMedidaRemote;

import java.util.Hashtable;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

import javax.naming.Context;

import javax.naming.InitialContext;
import javax.naming.NamingException;

public class MedidaValidator implements Validator {
    
    private LN_SFMedidaRemote lN_SFMedidaRemote;
    
    @Override
    public void validate(FacesContext facesContext, UIComponent uiComponent, Object object) {
        
        String descripcion = object.toString();
        
        try {
            final Context context = getInitialContext();
            lN_SFMedidaRemote = (LN_SFMedidaRemote) context.lookup("map-LN_SFMedida#chemtools_logica.LN.IR.LN_SFMedidaRemote");
        }catch(Exception e){}
        
        boolean exi = lN_SFMedidaRemote.verificarDescrpDupli(descripcion);
        
        if (!exi) {
            FacesMessage fm = new FacesMessage("La descripcion de Medida ya existe");
            throw new ValidatorException(fm);
        }
        
    }
    
    private static Context getInitialContext() throws NamingException {
        Hashtable env = new Hashtable();
        // WebLogic Server 10.x/12.x connection details
        env.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
        env.put(Context.PROVIDER_URL, "t3://127.0.0.1:7101");
        return new InitialContext(env);
    }
}